package cn.itcast.controller;

import cn.itcast.pojo.Dept;
import cn.itcast.pojo.Result;
import cn.itcast.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

    /*获取所有数据*/
    @GetMapping
    public Result list() {
        List<Dept> deptList = deptService.list();

        return Result.success(deptList);
    }

    /*根据Id删除部门*/
    /*也可以用PathParm（javaEE规范）*/
    @DeleteMapping
    public Result deleteById(@RequestParam(value = "id",required = false) Integer id){
        log.info("删除了部门id={}",id);
        deptService.deleteById(id);
        return Result.success();
    }

    /*新增部门*/

    @PostMapping
    public Result save(@RequestBody Dept dept){
        log.info("添加了部门name={]",dept.getName());
        deptService.save(dept);
        return Result.success();
    }

    /*根据id查询*/
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    /*根据Id修改部门*/
    @PutMapping
    public Result updateById(@RequestBody Dept dept){
        log.info("修改部门"+dept.toString());
        deptService.updateById(dept);
        return Result.success();
    }

    @PutMapping("/s")
    public Result update(@RequestParam(defaultValue = "10") Integer id){
        return null;
    }
}
